<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备管理</title>
    <link rel="stylesheet" href="user.css">
</head>
<body>
    <h1>设备信息</h1>
    <div id="data-container"></div>
<form action="/modbus" method="post" name="modbus">
    <div id="register-box">
        <h1>选择设备</h1>
        <div class="form">
            <!-- 设备选择下拉框 -->
            <div class="item">
                <p>选择要连接的设备 (01: COil Status, 02: Input Status, 03: Holding Register, 04: Input Register):</p>
                <select name="device" id="device" required>
                    <option value="">请选择设备</option>
                    <option value="01">COil Status</option>
                    <option value="02">Input Status</option>
                    <option value="03">Holding Register</option>
                    <option value="04">Input Register</option>
                </select>
            </div>
            <!-- IP输入框 -->
            <div class="item">
                <input type="text" name="ip" placeholder="ip" id="ip" maxlength="10" required>
            </div>
            <!-- 端口输入框 -->
            <div class="item">
                <input type="text" name="port" placeholder="port" id="port" maxlength="10" required>
            </div>
            <!-- 起始输入框 -->
            <div class="item">
                <input type="text" name="start" placeholder="start" id="start" maxlength="10" required>
            </div>
            <!-- 读取数量输入框（可选填） -->
            <div class="item">
                <input type="text" name="count" placeholder="count" id="count" maxlength="10">
            </div>
        </div>
        <button type="submit">连接</button>
    </div>
</form>
<br><br><button onclick="navigateToLink1()">菜单栏</button>
</body>
<script>
        // 定义一个函数来执行Ajax请求
        function fetchData() {
            const xhr = new XMLHttpRequest(); // 创建一个新的XMLHttpRequest对象
            xhr.open('GET', '/modbus', true); // 配置请求的类型、URL和是否异步

            // 设置请求完成后的回调函数
            xhr.onload = function() {
                if (xhr.status >= 200 && xhr.status < 300) {
                    // 解析返回的JSON数据
                    const data = JSON.parse(xhr.responseText);

                    // 获取要显示数据的HTML元素
                    const dataContainer = document.getElementById('data-container');

                    // 动态创建HTML内容并添加到元素中
                    let content = `<p>Id: ${data}</p>`;
                    content += `<p>Username: ${data}</p>`;
                    content += `<p>Email: ${data.Email}</p>`;
                    content += `<p>Phone: ${data.Phone}</p>`; // 假设返回的JSON中包含phone字段
                    content += `<p>Role: ${data.Role}</p>`;

                    dataContainer.innerHTML = content;
                } else {
                    console.error('请求失败，状态码：', xhr.status);
                }
            };

            // 发送请求
            xhr.send();
        }

        // 调用函数获取数据
    document.getElementById('link').addEventListener('click', fetchData);

        //  回到菜单页
    function navigateToLink1() {
            window.location.href = "http://127.0.0.1:8080/static/menu.html";
        }
</script>
</html>